package com.dgut.service.background.controller;

import com.alibaba.fastjson.JSON;
import com.dgut.service.background.config.resultConfig.JsonResult;
import com.dgut.service.background.config.resultConfig.ResultTool;
import com.dgut.service.background.entity.Exception;
import com.dgut.service.background.entity.Inspector;
import com.dgut.service.background.entity.User;
import com.dgut.service.background.service.ExceptionService;
import com.dgut.service.background.service.InspectorService;
import com.dgut.service.background.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 预警
 */
@RestController
@RequestMapping("exception")
public class ExceptionController {
    @Autowired
    private ExceptionService exceptionService;

    @Autowired
    UserService userService;

    @Autowired
    InspectorService inspectorService;

    @PostMapping("sendException")
    public String sendException(Exception e){
        User user = userService.findByOpenId(e.getUserOpenid());
        Inspector inspector = inspectorService.findByOpenId(e.getInspectorOpenid());
        //判断用户和检察员账号是否已注销
        if (user.getUserId()==0 || inspector.getInspectorId() == 0){
            JsonResult result = ResultTool.fail();
            return JSON.toJSONString(result);
        }
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = df.format(new Date());
        e.setTimes(time);
        exceptionService.SaveException(e);
        JsonResult result = ResultTool.success();
        return JSON.toJSONString(result);
    }
}
